package com.xuecheng.manage_cms.controller;

import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_cms.service.CmsPageService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * 用作CmsPage的预览页面的控制器
 */
@Controller
@RequestMapping("/cms/preview")
public class CmsPagePreviewController extends BaseController implements com.xuecheng.api.cms.CmsPagePreviewControllerApi {
    @Resource
    CmsPageService cmsPageServiceImpl;

    // 页面预览
    @RequestMapping(value = "/{pageId}", method = RequestMethod.GET)
    public void preview(@PathVariable("pageId") String pageId) {
        String pageHtml = cmsPageServiceImpl.getPageHtml(pageId);
        ServletOutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
            response.setContentType("text/html; charset=utf-8");
            outputStream.write(pageHtml.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
